package cn.iocoder.yudao.module.famous.doctor.service.FamNotice;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.famous.doctor.controller.app.famNoticeClose.vo.AppFamNoticeCloseBaseVO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.famNoticeClose.FamNoticeCloseDO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.famNoticeClose.FamNoticeCloseMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.FamNotice.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.FamNotice.FamNoticeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.FamNotice.FamNoticeConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.FamNotice.FamNoticeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 公告 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FamNoticeServiceImpl implements FamNoticeService {

    @Resource
    private FamNoticeMapper famNoticeMapper;
    @Resource
    private FamNoticeCloseMapper famNoticeCloseMapper;

    @Override
    public Long createFamNotice(FamNoticeCreateReqVO createReqVO) {
        // 插入
        FamNoticeDO famNotice = FamNoticeConvert.INSTANCE.convert(createReqVO);
        famNoticeMapper.insert(famNotice);
        // 返回
        return famNotice.getId();
    }

    @Override
    public void updateFamNotice(FamNoticeUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamNoticeExists(updateReqVO.getId());
        // 更新
        FamNoticeDO updateObj = FamNoticeConvert.INSTANCE.convert(updateReqVO);
        famNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamNotice(Long id) {
        // 校验存在
        validateFamNoticeExists(id);
        // 删除
        famNoticeMapper.deleteById(id);
    }

    private void validateFamNoticeExists(Long id) {
        if (famNoticeMapper.selectById(id) == null) {
            throw exception(FAM_NOTICE_NOT_EXISTS);
        }
    }

    @Override
    public FamNoticeDO getFamNotice(Long id) {
        return famNoticeMapper.selectById(id);
    }

    @Override
    public FamNoticeDO getFamNoticeByClientType(Byte clientType,Long userId) {
        if (clientType == null){
            clientType = 1;//默认用户端公告
        }
        List<Long> ignoreNoticeIds = new ArrayList<>();
        if (userId != null){
            // 排除用户已经关闭的公告
            List<FamNoticeCloseDO> famNoticeCloseDOS = famNoticeCloseMapper.selectList(new LambdaQueryWrapperX<FamNoticeCloseDO>()
                    .eq(FamNoticeCloseDO::getUserId, userId)
            );
            if (famNoticeCloseDOS != null && famNoticeCloseDOS.size()>0){
                ignoreNoticeIds = famNoticeCloseDOS.stream()
                        .map(FamNoticeCloseDO::getId)
                        .collect(Collectors.toList());
            }
        }
        return famNoticeMapper.selectOne(new LambdaQueryWrapperX<FamNoticeDO>()
                .eq(FamNoticeDO::getClientType,clientType)
                .eq(FamNoticeDO::getStatus,1)
                .ne(ignoreNoticeIds.size()>0,FamNoticeDO::getId,ignoreNoticeIds)
                .orderByDesc(FamNoticeDO::getUpdateTime)
                .last("limit 1")
        );
    }

    @Override
    public void closeNotice(AppFamNoticeCloseBaseVO appFamNoticeCloseBaseVO) {
        // 先查询，是否已经关闭
        Long num = famNoticeCloseMapper.selectCount(new LambdaQueryWrapperX<FamNoticeCloseDO>()
                .eq(FamNoticeCloseDO::getNoticeId, appFamNoticeCloseBaseVO.getNoticeId())
                .eq(FamNoticeCloseDO::getUserId, appFamNoticeCloseBaseVO.getUserId()));
        if (num == 0){
            famNoticeCloseMapper.insert(new FamNoticeCloseDO()
                    .setNoticeId(appFamNoticeCloseBaseVO.getNoticeId())
                    .setUserId(appFamNoticeCloseBaseVO.getUserId()));
        }
    }

    @Override
    public List<FamNoticeDO> getFamNoticeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famNoticeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamNoticeDO> getFamNoticePage(FamNoticePageReqVO pageReqVO) {
        return famNoticeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamNoticeDO> getFamNoticeList(FamNoticeExportReqVO exportReqVO) {
        return famNoticeMapper.selectList(exportReqVO);
    }

}
